home *** CD-ROM | disk | FTP | other *** search
/ io Programmo 60 / IOPROG_60.ISO / soft / c++ / gsl-1.1.1-setup.exe / {app} / src / ntuple / test.c < prev   
Encoding:
C/C++ Source or Header  |  2001-08-22  |  3.4 KB  |  158 lines

  1. #include <config.h>
  2. #include <stdlib.h>
  3. #include <gsl/gsl_ntuple.h>
  4. #include <gsl/gsl_test.h>
  5.  
  6. struct data
  7. {
  8.   int num;
  9.   double x;
  10.   double y;
  11.   double z;
  12. };
  13. int sel_func (void *ntuple_data, void * params);
  14. double val_func (void *ntuple_data, void * params);
  15.  
  16. int
  17. main (void)
  18. {
  19.   struct data ntuple_row;
  20.   int i;
  21.  
  22.   double x[1000], y[1000], z[1000], f[100];
  23.  
  24.   gsl_ntuple_select_fn S;
  25.   gsl_ntuple_value_fn V;
  26.   
  27.   double scale = 1.5;
  28.   
  29.   S.function = &sel_func;
  30.   S.params = &scale;
  31.   
  32.   V.function = &val_func;
  33.   V.params = &scale;
  34.  
  35.   {
  36.     gsl_ntuple *ntuple = gsl_ntuple_create ("test.dat", &ntuple_row, 
  37.                                             sizeof (ntuple_row));
  38.  
  39.     int status = 0;
  40.  
  41.     for (i = 0; i < 100; i++) f[i] = 0;
  42.     
  43.     for (i = 0; i < 1000; i++)
  44.       {
  45.         double xi = 1.0 / (i + 1.0);
  46.         double yi = xi * xi ;
  47.         double zi = xi * xi * xi;
  48.         
  49.         ntuple_row.x = xi;
  50.         ntuple_row.y = yi;
  51.         ntuple_row.z = zi;
  52.         ntuple_row.num = i;
  53.         
  54.         x[i] = xi; y[i] = yi; z[i] = zi;
  55.         
  56.         if (xi * scale < 0.1)
  57.           {
  58.             double v = xi + yi + zi;
  59.             int k = (int)(100.0*v*scale);
  60.             f[k]++;
  61.           }
  62.  
  63.         /* printf ("x,y,z = %f,%f,%f; n=%x \n", ntuple_row.x,
  64.            ntuple_row.y, ntuple_row.z, ntuple_row.num); */
  65.         
  66.         {
  67.           int s = gsl_ntuple_bookdata (ntuple);
  68.  
  69.           if (s != GSL_SUCCESS)
  70.             {
  71.               status = 1;
  72.             }
  73.         }
  74.       }
  75.     
  76.     gsl_ntuple_close (ntuple);
  77.  
  78.     gsl_test (status, "writing ntuples");
  79.   }
  80.  
  81.   {
  82.     gsl_ntuple *ntuple = gsl_ntuple_open ("test.dat", &ntuple_row, 
  83.                                           sizeof (ntuple_row));
  84.     int status = 0;
  85.  
  86.     for (i = 0; i < 1000; i++)
  87.       {
  88.         gsl_ntuple_read (ntuple);
  89.  
  90.         status = (ntuple_row.num != i);
  91.         status |= (ntuple_row.x != x[i]);
  92.         status |= (ntuple_row.y != y[i]);
  93.         status |= (ntuple_row.z != z[i]);
  94.  
  95.         /* printf ("x,y,z = %f,%f,%f; n=%d\n", ntuple_row.x,
  96.                 ntuple_row.y, ntuple_row.z, ntuple_row.num); */
  97.       }
  98.     gsl_ntuple_close (ntuple);
  99.  
  100.     gsl_test (status, "reading ntuples");
  101.   }    
  102.  
  103.   {
  104.     int status = 0;
  105.  
  106.     gsl_ntuple *ntuple = gsl_ntuple_open ("test.dat", &ntuple_row, 
  107.                                           sizeof (ntuple_row));
  108.  
  109.     gsl_histogram *h = gsl_histogram_calloc_uniform (100, 0., 1.);
  110.  
  111.     gsl_ntuple_project (h, ntuple, &V, &S);
  112.  
  113.     /* gsl_histogram_fprintf (stdout, h, "%f", "%f"); */
  114.  
  115.     for (i = 0; i < 100; i++)
  116.       {
  117.         /* printf ("h  %g f  %g\n", h->bin[i], f[i]); */
  118.  
  119.         if (h->bin[i] != f[i])
  120.           {
  121.             status = 1;
  122.           }
  123.       }
  124.  
  125.     gsl_test (status, "histogramming ntuples");
  126.  
  127.     gsl_histogram_free (h);
  128.   }
  129.  
  130.   exit (gsl_test_summary());
  131. }
  132.  
  133. int
  134. sel_func (void *ntuple_data, void * params)
  135. {
  136.   double x, y, z, scale;
  137.   scale = *(double *)params;
  138.  
  139.   x = ((struct data *) ntuple_data)->x;
  140.   y = ((struct data *) ntuple_data)->y;
  141.   z = ((struct data *) ntuple_data)->z;
  142.  
  143.   return (x*scale < 0.1);
  144. }
  145.  
  146. double
  147. val_func (void *ntuple_data, void * params)
  148. {
  149.   double x, y, z, scale;
  150.   scale = *(double *)params;
  151.  
  152.   x = ((struct data *) ntuple_data)->x;
  153.   y = ((struct data *) ntuple_data)->y;
  154.   z = ((struct data *) ntuple_data)->z;
  155.  
  156.   return (x + y + z) * scale;
  157. }
  158.